import AsyncStorage from "@react-native-async-storage/async-storage";

export const getRedRepubDB = async () => {
    let RedRepubDB = await AsyncStorage.getItem("RedRepubDB") || "[]";
    let RedRepub = JSON.parse(RedRepubDB) || [];
    return RedRepub;
}

export const setRedRepubDB = async (date:string) => {
    await AsyncStorage.setItem("RedRepubDB",date);
    console.log(`RedRepubDB保存后的数据：${date}`)
}
//查
export const getHashRedRepub = async (hash: string) => {
    let RedRepub = await getRedRepubDB();
    for(let i = 0 ; i < RedRepub.length ; i++)
    {
        if(RedRepub[i].hash===hash)
        {
            return RedRepub[i];
        }
    }
    return false;
}
//改 false代表不存在   true代表修改成功
export const updateRedRepub = async (date:any) => {
    let RedRepub = await getRedRepubDB();
    for(let i = 0 ; i < RedRepub.length ; i++)
    {
        if(RedRepub[i].hash===date.hash)
        {
            RedRepub[i] = date;
            await AsyncStorage.setItem("RedRepubDB",JSON.stringify(RedRepub));
            return true;
        }
    }
    return false;
}
//增  false代表已存在 true 代表添加成功
export const addRedRepub = async (date:any) => {
    let RedRepub = await getRedRepubDB();
    for(let i = 0 ; i < RedRepub.length ; i++)
    {
        if(RedRepub[i].hash===date.hash)
        {
            return false;
        }
    }
    RedRepub.push(date);
    await AsyncStorage.setItem("RedRepubDB",JSON.stringify(RedRepub));
    return true;
}
//删
export const delRedRepub = async (date:any) => {
    let RedRepub = await getRedRepubDB();
    for(let i = 0 ; i < RedRepub.length ; i++)
    {
        if(RedRepub[i].hash===date.hash)
        {
            RedRepub.splice(i,1)
            await AsyncStorage.setItem("RedRepubDB",JSON.stringify(RedRepub));
            return true;
        }
    }
    return false;
}

